package com.njupt.user.model;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.rmi.ServerException;

/**
 * Created by ASUS on 2019/4/25.
 */
public class FileSend {
    public  String pop(HttpServletRequest request, HttpServletResponse response, String username, String test_name) throws ServletException, IOException {
        //定义上传文件的最大字节
        int MAX_SIZE = 102400 * 102400;
        //创建根路径的保存变量
        String rootPath;
        //声明文件读入类
        DataInputStream in = null;
        FileOutputStream fileOut = null;
        //取得互联网程序的绝对地址
        String realPath = request.getSession().getServletContext().getRealPath("/");
        realPath = realPath.substring(0, realPath.indexOf("\\target"));
//    out.print(realPath);
        //创建文件的保存目录
        rootPath = realPath + "\\web\\upload\\";
        //取得客户端上传的数据类型
        String contentType = request.getContentType();
        try {
            if (contentType.indexOf("multipart/form-data") >= 0) {
                //读入上传数据
                in = new DataInputStream(request.getInputStream());
                int formDataLength = request.getContentLength()+1024;
                if (formDataLength > MAX_SIZE) {
                    return "outLength";
                }
                //保存上传文件的数据
                byte dataBytes[] = new byte[formDataLength];
                int byteRead = 0;
                int totalBytesRead = 0;
                //上传的数据保存在byte数组里面
                in.read(dataBytes);
//                while (totalBytesRead < formDataLength) {
//                    byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
//                    totalBytesRead += byteRead;
//                }
                //根据byte数组创建字符串
                String file = new String(dataBytes, "utf-8");
                //System.out.println(file);
                int lastIndex = contentType.lastIndexOf("=");
                //取得数据的分隔字符串
                String boundary = contentType.substring(lastIndex + 1, contentType.length());
                //创建保存路径的文件名
                String fileName = rootPath + username+"_"+test_name+".txt";
                int pos;
                pos = file.indexOf("filename=\"");
                pos = file.indexOf("\n", pos) + 1;
                pos = file.indexOf("\n", pos) + 1;
                pos = file.indexOf("\n", pos) + 1;
                int boundaryLocation = file.indexOf(boundary, pos) - 4;
                //取得文件数据的开始的位置
               // System.out.println(file.length());
                //System.out.println(boundaryLocation);
                int startPos = ((file.substring(0, pos)).getBytes()).length;
                int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
                File checkFile = new File(fileName);
                if (checkFile.exists()) {
                    checkFile.delete();
                }
                //检查上传文件的目录是否存在
                File fileDir = new File(rootPath);
                if (!fileDir.exists()) {
                    fileDir.mkdirs();
                }
                //创建文件的输出类
                fileOut = new FileOutputStream(fileName);
                //保存文件的数据
                fileOut.write(dataBytes, startPos, (endPos - startPos));
                fileOut.close();
                return "success";
            } else {
                String content = request.getContentType();
                return "notType";
            }
        } catch (Exception ex) {
            throw new ServerException(ex.getMessage());
        }
    }
}
